<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-button type='primary' style="margin-bottom:20px;" @click="showAddCateDialog">添加分类</el-button>
      <!-- 树形控件 -->
      <tree-table :data='catelist' :columns='columns' style="margin-top:15px" :selection-type='false' show-index
        index-text='#' border :show-row-hover='false' :expand-type='false'>

        <template slot='isok' slot-scope='scope'>
          <i class="el-icon-success" v-if='scope.row.cat_deleted===false' style="color:green"></i>
          <i class="el-icon-danger" v-else style="color:red"></i>
        </template>

        <template slot='order' slot-scope="scope">
          <el-tag v-if='scope.row.cat_level === 0'>一级</el-tag>
          <el-tag type="success" v-else-if='scope.row.cat_level === 1'>二级</el-tag>
          <el-tag type="warning" v-else>三级</el-tag>
        </template>

        <template slot='opt'>
          <el-button type="primary" size="mini" class="el-icon-edit quxiao">编辑</el-button>
          <el-button type="danger" size="mini" class="el-icon-delete">删除</el-button>
        </template>
      </tree-table>

      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[2, 4, 8, 16]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>

      <!-- 对话框 -->
      <el-dialog title='商品分类' :visible.sync='cateAddDialogVisible' width='50%' @close='addCateDialogClosed'>
        <el-form :model="addCateForm" :rules="addCateRules" ref="addCateRef" label-width="100px" class="demo-ruleForm">
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addCateForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label='父级分类'>
            <el-cascader :options="parentCateList" :props="cascaderProps" @change="handleChange" v-model="selectedkeys"
              clearable>
            </el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer">
          <el-button @click="cateAddDialogVisible = false" class="quxiao">Cancel</el-button>
          <el-button type="primary" @click="addCate">OK</el-button>
        </span>
      </el-dialog>

    </el-card>
  </div>
</template>

<script scoped>
import { cateListAPI, addCateListAPI } from '@/api/categories'
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 局部注册的组件
  components: {},
  // 组件状态值
  data () {
    return {
      selectedkeys: [],
      cascaderProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true
      },
      parentCateList: [],
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_lavel: 0
      },
      addCateRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 添加对话框显示和隐藏
      cateAddDialogVisible: false,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        }, {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        }, {
          label: '排序',
          type: 'template',
          template: 'order'
        }, {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      // 列表数据
      catelist: [],
      queryInfo: {
        pagenum: 1, // 页数
        pagesize: 2 // 每页多少条
      },
      total: 0 // 总条数
    }
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    addCateDialogClosed () {
      this.$refs.addCateRef.resetFields()
      this.selectedkeys = []
      this.addCateForm.cat_lavel = 0
      this.addCateForm.cat_pid = 0
    },
    addCate () {
      this.$refs.addCateRef.validate(async valid => {
        if (!valid) return
        await addCateListAPI(this.addCateForm)
        this.$message({
          message: '添加分类成功',
          type: 'success'
        })
        this.getcatelist()
        this.cateAddDialogVisible = false
      })
    },
    handleChange () {
      if (this.selectedkeys.length > 0) {
        this.addCateForm.cat_pid = this.selectedkeys[this.selectedkeys.length - 1]
        this.addCateForm.cat_lavel = this.selectedkeys.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_lavel = 0
      }
    },
    showAddCateDialog () {
      this.getParentCateList()
      this.cateAddDialogVisible = true
    },
    async getParentCateList () {
      const { data: res } = await cateListAPI({ type: 2 })
      this.parentCateList = res.data
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getcatelist()
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getcatelist()
    },
    async getcatelist () {
      const { data: res } = await cateListAPI(this.queryInfo)
      this.catelist = res.data.result
      this.total = res.data.total
    }
  },
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    this.getcatelist()
  },
  /**
  * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
  */
  mounted () {
  }
}
</script>

<style scoped>
.el-cascader {
  width: 100%;
}
.el-cascader-menu {
  height: 300px;
}
</style>
